/*
 * Copyright 2010 Christian Schindelhauer, Peter Thiemann, Faisal Aslam, Luminous Fennell and Gidon Ernst.
 * All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 3 for more details (a copy is
 * included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU General Public License
 * version 3 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Faisal Aslam
 * (aslam AT informatik.uni-freibug.de or studentresearcher AT gmail.com)
 * if you need additional information or have any questions.
 */
package takatuka.vm.autoGenerated.forExceptionPrettyPrint;

import takatuka.classreader.dataObjs.*;
import takatuka.classreader.logic.logAndStats.LogHolder;
import takatuka.tukFormat.dataObjs.*;

/**
 * <p>Title: </p>
 * <p>Description:
 *
 * It generate three properties files with class, method names and line numbers.
 * These files will be used to generate exceptions information/messages.
 *
 * </p>
 * @author Faisal Aslam
 * @version 1.0
 */
public class GenerateExceptionData {

    private static final GenerateExceptionData myObj = new GenerateExceptionData();
    private static boolean isExecuted = false;

    /**
     * 
     */
    private GenerateExceptionData() {
    }

    public static GenerateExceptionData getInstanceOf() {
        return myObj;
    }
    public int getMethodID(String methodName) {
        
        return MethodNamesPropertyFile.getInstanceOf().
                methodNameToID.get(methodName);
    }
    public void generate() {
        if (isExecuted) {
            return; //execute only once
        }
        isExecuted = true;
        ClassFileController cFileContr = ClassFileController.getInstanceOf();
        MethodNamesPropertyFile mNameGenerator = MethodNamesPropertyFile.getInstanceOf();
        FieldNamesPropertyFile fNameGenerator = FieldNamesPropertyFile.getInstanceOf();
        ClassNamePropertyFile classNameGenerator = ClassNamePropertyFile.getInstanceOf();
        LineNumberPropertyFile lineNumberGenerator = LineNumberPropertyFile.getInstanceOf();
        LogHolder.getInstanceOf().addLog("Generating line number, method-names property files");

        for (int classContrIndex = 0; classContrIndex < cFileContr.getCurrentSize(); classContrIndex++) {
            ClassFile cFile = (ClassFile) cFileContr.get(classContrIndex);

            classNameGenerator.execute(cFile);

            MethodInfoController mContr = cFile.getMethodInfoController();
            for (int methodContrIndex = 0; methodContrIndex < mContr.getCurrentSize(); methodContrIndex++) {
                LFMethodInfo methodInfo = (LFMethodInfo) mContr.get(methodContrIndex);

                mNameGenerator.execute(methodInfo, cFile);

                lineNumberGenerator.execute(methodInfo);
            }

            FieldInfoController fContr = cFile.getFieldInfoController();
            for (int fieldContrIndex = 0; fieldContrIndex < fContr.getCurrentSize(); fieldContrIndex++) {
                LFFieldInfo fieldInfo = (LFFieldInfo) fContr.get(fieldContrIndex);

                fNameGenerator.execute(fieldInfo, cFile);
            }

        }
        LogHolder.getInstanceOf().addLog("Line number and method names property file is generated");
        mNameGenerator.writeInPropertyFile();
        fNameGenerator.writeInPropertyFile();
        classNameGenerator.writeInPropertyFile();
        lineNumberGenerator.writeInPropertyFile();
    }
}
